"""
Case Type   : 兼容函数
Case Name   : 验证conv()函数与其他数学函数的交互使用
Create At   : 2023.07.19
Owner       : z@wan005
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、验证conv()函数与其他数学函数的交互使用
    3、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、清理成功
History     :modified by z@wan005，2023.08.15，yat执行结果的精度处理和手工执行
             结果不一致， 修改用例为py确保执行结果同手工执行结果一致
             modified by z@wan005，2023.08.18，修改用例，指定执行的数据库为兼容b库
             modified by zhanghuan96,2024.6.20，受I90QKD代码影响，精度变更
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest06(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_conv_0006'
        self.common = Common()

    def test_create_function(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      sql_res, '兼容B库创建失败')

        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:验证conv()函数与其他数学函数的交互使用;expect:成功-----'
        self.log.info(text)
        sql_cmd2 = f"select abs(conv('22C',16,10));" \
                   f"select atan(conv(5,10,2));" \
                   f"select atan2(conv(3,10,10),2);" \
                   f"select bitand(conv(3,10,10),conv(3,10,11));" \
                   f"select cbrt(conv('22F',16,8));" \
                   f"select ceil(cbrt(conv('22F',16,8)));" \
                   f"select cos(conv('21',15,8));" \
                   f"select cot(conv(3,10,10));" \
                   f"select degrees(conv(28,15,8));" \
                   f"select div(conv(28,14,8),3);" \
                   f"select exp(conv(28,14,8));" \
                   f"select floor(conv('28d',14,8));" \
                   f"select radians(conv(11001,2,10));" \
                   f"select ln(conv(28,14,7));" \
                   f"select mod(conv('c',14,7),5);" \
                   f"select power(3,conv('c',14,7));" \
                   f"select sign(conv('28c',17,8));" \
                   f"select sin(conv('2c',14,9));" \
                   f"select sqrt(conv(1101,2,10));" \
                   f"select tan(conv(100101,2,10));" \
                   f"select cast(conv('a5',11,30) as text);" \
                   f"select \"numeric\"(cast(conv('a5',11,30) as char));" \
                   f"select oid(cast(conv('a5',11,30) as char));"
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(sql_res2)
        msg_lis = ['556', '1.560895660206908', '0.9827937232473291', '3',
                   '10.18650014651848', '11', '0.7654140519453434',
                   '12851600114359308276', '2635.605857601787', '14',
                   '12851600114359308276', '1005', '0.4363323129985824',
                   '3.9318256327243258', '0', '14348907', '1',
                   '0.01770192510541358', '3.605551275463989',
                   '-0.8407712554027597', '3P', '3', '3']
        for msg in msg_lis:
            self.assertIn(msg, sql_res2, '执行失败')

        sql_cmd3 = "select float4(conv(5,10,2)),float8(conv(5,10,2));"
        self.log.info(sql_cmd3)
        sql_res3 = self.sh_primary.execut_db_sql(sql_cmd3, dbname=self.db_name)
        self.log.info(sql_res3)
        rows, _ = self.common.parse_sql_query_res(sql_res3)
        expect = [['101', '101']]
        self.assertEqual(rows, expect, '执行失败' + text)

        sql_cmd4 = "select int1(conv(1101,2,10)),int2(conv(11001,2,10))," \
                   "int4(conv(10101,2,10)),int16(conv(1010111,2,10));"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4, dbname=self.db_name)
        self.log.info(sql_res4)
        rows, _ = self.common.parse_sql_query_res(sql_res4)
        expect = [['13', '25', '21', '87']]
        self.assertEqual(rows, expect, '执行失败' + text)

        sql_cmd5 = "select log(conv(28,14,7)),log(5,conv(28,14,7));"
        self.log.info(sql_cmd5)
        sql_res5 = self.sh_primary.execut_db_sql(sql_cmd5, dbname=self.db_name)
        self.log.info(sql_res5)
        rows, _ = self.common.parse_sql_query_res(sql_res5)
        expect = [['3.931825632724326', '2.4429806222085733']]
        self.assertEqual(rows, expect, '执行失败' + text)

        sql_cmd6 = "select round(conv('28d',14,8)),round(conv('28d',14,8),5);"
        self.log.info(sql_cmd6)
        sql_res6 = self.sh_primary.execut_db_sql(sql_cmd6, dbname=self.db_name)
        self.log.info(sql_res6)
        rows, _ = self.common.parse_sql_query_res(sql_res6)
        expect = [['1005', '1005.00000']]
        self.assertEqual(rows, expect, '执行失败' + text)

        sql_cmd7 = "select trunc(conv(9,11,8)),trunc(conv(9,11,9),5);"
        self.log.info(sql_cmd7)
        sql_res7 = self.sh_primary.execut_db_sql(sql_cmd7, dbname=self.db_name)
        self.log.info(sql_res7)
        rows, _ = self.common.parse_sql_query_res(sql_res7)
        expect = [['11', '10.00000']]
        self.assertEqual(rows, expect, '执行失败' + text)

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      sql_res4, '清理环境失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
